<template>
  <section>
    <!-- 面包屑 -->
 <breadcrumbs></breadcrumbs>
    <!-- 查询条件 -->
    <el-form ref="searchForm"
             :model="searchForm"
             label-width="100px"
             inline
             class="demo-form-inline"
             size='small'
             style="border-bottom: 1px solid #f6f6f6;">
      <el-form-item label="配置名称"
                    prop="name">
        <el-input v-model="searchForm.name"
                  placeholder="请输入配置名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"
                   @click="search">查询</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>
    <btns @exec="exec"></btns>
    <div class="data-container scroll-table">
      <div>
        <div class="ind-title lh50 f16">配置列表</div>
      </div>
      <!-- 配置列表 -->
      <el-table :data="tableData"
                border
                style="width: 100%"
                @selection-change="tableSelectionChange">
        <el-table-column type="selection"
                         width="40">
        </el-table-column>
        <el-table-column prop="configKey"
                         label="配置名称">
        </el-table-column>
        <el-table-column prop="configValue"
                         label="配置值">
        </el-table-column>
        <el-table-column prop="ds"
                         label="描述">
        </el-table-column>
      </el-table>
    </div>
    <!--新增修改界面-->
    <el-dialog :title="funTit=='add'?'新增':'修改'"
               :visible.sync="addORUpdateVisible"
               width="700px"
               :close-on-click-modal="false">
      <el-form :model="configForm"
               label-width="80px"
               :rules="rules"
               ref="configForm">
        <el-form-item label="配置名称"
                      prop="configKey">
          <el-input v-model="configForm.configKey"
                    auto-complete="off"
                    :disabled="configForm.configId"></el-input>
        </el-form-item>
        <el-form-item label="配置值"
                      prop="configValue">
          <el-input v-model="configForm.configValue"
                    auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="描述"
                      prop="ds">
          <el-input type="textarea"
                    v-model="configForm.ds"
                    auto-complete="off"
                    :disabled="configForm.configId"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click.native="addORUpdateVisible = false">取消</el-button>
        <el-button type="primary"
                   @click.native="submit('configForm')">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>

import btns from '@/components/btns.vue'
import breadcrumbs from "@/components/breadcrumbs.vue";

export default {
  components: {
    btns,
    breadcrumbs,
  },
  data () {
    return {
      searchForm: {
        name: ''
      },
      addORUpdateVisible: false,
      funTit: '',
      configForm: {
        "configKey": "",
        "configValue": "",
        "ds": "",
        "pri": 0,
        "type": 0
      },
      rules: {
        "configKey": [
          { required: true, message: '请输入配置名称', trigger: 'blur' }
        ],
        "configValue": [
          { required: true, message: '请输入配置值', trigger: 'blur' }
        ],
        "ds": [
          { required: true, message: '请输入描述', trigger: 'blur' }
        ],
      },
      tableData: [],
      tableSelection: []
    }
  },
  created () {
    this.configlist();
  },
  methods: {
    configlist () {
      //获取接口
      this.$api.config.list({}).then(d => {
        if (d.code == 200) {
          this.tableData = d.data;
        } else {
          this.$message.error(d.msg);
        }
      }).catch((e) => {
        console.log(e);
      })
    },
    handleReset () {
      this.$refs.searchForm.resetFields();
      this.search()
    },
    search () {
      if (this.searchForm.name) {
        this.tableData = this.tableData.filter(e => e.configKey.indexOf(this.searchForm.name) >= 0);
      } else {
        this.configlist();
      }
    },
    rolelist () {
      //获取接口
      this.$api.role.list({}).then(d => {
        if (d.code == 200) {
          this.tableData = d.data;
        } else {
          this.$message.error(d.msg);
        }
      }).catch((e) => {
        console.log(e);
      })
    },
    tableSelectionChange (val) {
      this.tableSelection = val;
    },
    exec (fun) {
      if (this[fun]) {
        this.funTit = fun;
        this[fun]()
      } else {
        this.$message.error('方法未定义');
      }
    },
    add () {
      this.configForm = {
        "configKey": "",
        "configValue": "",
        "ds": "",
        "pri": 0,
        "type": 0
      }
      this.addORUpdateVisible = true;
    },
    update () {
      if (this.tableSelection.length != 1) {
        this.$message.error('请选中一行数据');
        return
      }

      this.configForm = this.tableSelection[0];
      this.addORUpdateVisible = true;
    },
    refresh () {
      this.configlist();
    },
    submit (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(this.configForm)
          if (this.funTit == 'add') {
            this.$api.config.add(this.configForm).then(d => {
              if (d.code == 200) {
                this.addORUpdateVisible = false;
                this.$refs.configForm.resetFields();
                this.$message.success('新增成功');
                this.configlist();
              } else {
                this.$message.error(d.msg);
              }
            }).catch((e) => {
              console.log(e);
            })
          } else {
            this.$api.config.update(this.configForm).then(d => {
              if (d.code == 200) {
                this.addORUpdateVisible = false;
                this.$refs.configForm.resetFields();
                this.$message.success('修改成功');
                this.configlist();
              } else {
                this.$message.error(d.msg);
              }
            }).catch((e) => {
              console.log(e);
            })
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
}

</script>